package com.xw.comm.core;

import com.xw.comm.entity.map.BaseMapObject;
import com.xw.comm.entity.run.BaseObject;
import com.xw.comm.entity.run.BaseStateEnum;
import com.xw.comm.entity.task.CreateTarget;
import com.xw.comm.entity.task.TaskTarget;
import com.xw.comm.service.map.BackgroundMapService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xiongwei_vendor
 * @date 2021/9/30 16:48
 */
@Slf4j
@Component
public class WorkTarget {

    @Autowired
    private BackgroundMapService mapService;

    /**
     * 本次任务目标
     */
    private TaskTarget taskTarget;

    /**
     * 创建的任务目标
     */
    private CreateTarget createTarget;

    private List<String> mapIdList;

    public TaskTarget getTaskTarget() {
        return taskTarget;
    }

    public void setTaskTarget(TaskTarget taskTarget) {
        this.taskTarget = taskTarget;
    }

    public CreateTarget getCreateTarget() {
        return createTarget;
    }

    public void setCreateTarget(CreateTarget createTarget) {
        this.createTarget = createTarget;
    }

    /**
     * 通过主要对象的背景，实现
     *
     * @param baseObject 基础对象
     */
    public void createTargetByMainInfo(BaseObject baseObject) {

        BaseMapObject mainMapObject = baseObject.getBaseMapObject();
        if (taskTarget == null) {
            log.info("任务还没有创建");
            return;
        }
        BaseMapObject taskMapObject = taskTarget.getBaseMapObject();

        if (null == mapIdList || mapIdList.isEmpty()) {
            List<String> mapServiceIdList = mapService.findTaskRelationshipIdList(mainMapObject.getObjectId(), taskMapObject.getObjectId());
            mapIdList = new ArrayList<>(mapServiceIdList);
        }

        if (null != createTarget){
            if (createTarget.getBaseMapObject().getObjectId().equals(mainMapObject.getObjectId())){
                return;
            }
        }

        for (int i = 1; i < mapIdList.size(); i++) {
            if (mapIdList.get(i - 1).equals(mainMapObject.getObjectId())) {
                BaseMapObject baseMapObject = mapService.getMapObjectById(mapIdList.get(i));
                createTarget = new CreateTarget();
                createTarget.setBaseMapObject(baseMapObject);
                createTarget.setBaseStateEnum(BaseStateEnum.NON_COMBAT);
            }
        }


    }


}
